package com.su02.multi.examples.mlr;

import com.su02.multi.chainrule.Function;
import com.su02.multi.examples.gd.GradientDescent;
import com.su02.multi.util.List2MapUtil;

import org.apache.commons.lang3.RandomUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public final class Demo4MLR {
    public static void main(String[] args) {
        TrainMatrix4MLR mat = new TrainMatrix4MLR(entries(5));
        Function f = MultiLinearRegression.MSELossFunction(mat);
        Map<Integer, Double> start = List2MapUtil.convertList2Map(List.of(0., 0., 0.));
        System.out.println(GradientDescent.optimize(f, start, 0.001, 0));
    }

    private static List<TrainEntry4MLR> entries(int len) {
        List<TrainEntry4MLR> ans = new ArrayList<>(len);
        int p = 0;
        while (p < len) {
            double x0 = nextSign() * RandomUtils.secure().randomDouble(0., 5.);
            double x1 = nextSign() * RandomUtils.secure().randomDouble(0., 5.);
            ans.add(new TrainEntry4MLR(List.of(x0, x1), Math.PI * x0 + Math.E * x1 + 3.));
            p ++;
        }
        return ans;
    }

    private static double nextSign() {
        return RandomUtils.secure().randomBoolean() ? 1.0 : -1.0;
    }
}